<!-- 单柱状图折线图组合 -->
<template>
  <div class="hisorlineContainer">
    <div :id="`hisorline${getId}`" style="width: 100%; height: 100%" />
  </div>
</template>
  
  <script>
const echarts = require("echarts");

export default {
  name: "LineBarChart",
  props: {
    chartData: {
      type: Object,
    },
    isDataZoomShow: {
      type: Boolean,
      default: () => {
        return false;
      },
    },

    maxValueSpan: {
      type: Number,
      default: () => {
        return 6;
      },
    },
    getId: {
      type: String,
      default: "_",
    },
    barColorList: {
      type: Array,
    },
    isBgColorShow: {
      type: Boolean,
    },
    xNewlineNum: {
      type: Number,
      default: 7,
    },
    // 重点标记
    isImport: {
      type: Array,
      default: () => {
        return [];
      },
    },
    optionObj: {
      type: Object,
      default: () => {
        return {
          grid: {
            top: "10%",
            left: "8%",
            bottom: "7%",
            right: "8%",
            containLabel: true,
          },
          backgroundColor: ["#2177ff", "#2177ff"],
          opentimer: false,
        };
      },
    },
  },
  data() {
    return {
      chart: null,
      scoreOption: {},
      timer: "",
    };
  },
  watch: {
    chartData: {
      handler: function (val, oldVal) {
        // if (val !== oldVal) {
        this.scoreOption.xAxis.data = val.xData;
        this.scoreOption.legend.data = val.lendName;
        val.lendName.forEach((el, index) => {
          this.scoreOption.series[index].name = el;
          this.scoreOption.series[index].data = val["yData" + index];
        });
        this.chart.setOption(this.scoreOption);
        // }
      },
      deep: true,
    },
  },
  mounted() {
    this.draw();
    this.$nextTick(() => {
      //   this.initChart()
      this.chart.resize();
      this.chart = echarts.init(this.$el, "macarons");
      let self = this;
      let chart = this.chart;
      chart.on("click", (params) => {
        console.log(params);
        self.$emit("getLine", params);
      });
    });
  },
  destroyed() {
    window.removeEventListener("resize", this.onResize);
    window.onresize = null;
    clearInterval(this.timer);
    this.timer = "";
  },
  methods: {
    draw() {
      this.scoreOption = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
            label: {
              show: true,
            },
          },
        },
        grid: {
          left: "2%",
          top: "18%",
          right: "2%",
          bottom: "22%",
        },
        legend: {
          data: this.chartData.lendName,
          top: "4%",
          right: 10,
          textStyle: {
            color: "#848484", // 右上角文字颜色
            fontSize: "14",
          },
          itemWidth: 16,
          itemHeight: 16,
        },
        xAxis: {
          data: this.chartData.xData,
          type: "category",
          axisLabel: {
            // color: '#96A4F4'
            interval: 0,
            textStyle: {
              color: "#666666",
              fontFamily: "MicrosoftYaHei",
              fontSize: "18",
              lineHeight: "25",
            },
          },
          axisLine: {
            lineStyle: {
              color: "#999999",
            },
            width: 5,
          },
          axisTick: true, // 隐藏刻度代码
        },
        yAxis: [
          {
            type: "value",

            splitLine: {
              show: true,
              lineStyle: {
                type: "dashed", // 横线样式-虚线
                color: "#eee", // 横线颜色
              },
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false,
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: "#666666",
                fontSize: 14,
              },
            },
          },
          {
            type: "value",
            position: "right",
            splitLine: {
              show: true,
              lineStyle: {
                type: "dashed", // 横线样式-虚线
                color: "#eee", // 横线颜色
              },
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false,
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: "#666666",
                fontSize: 14,
              },
            },
          },
        ],
        series: [
          {
            name: "审核通过率",
            type: "bar",
            barWidth: 18,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 1,
                    color: "#1e7ef4",
                  },
                  {
                    offset: 1,
                    color: "#1e7ef4",
                  },
                ]),
                barBorderRadius: 4,
              },
            },
            data: this.chartData.yData0,
          },
          {
            name: "审核不通过率",
            type: "bar",
            barWidth: 18,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 1,
                    color: "#a75cfe",
                  },
                  {
                    offset: 1,
                    color: "#a75cfe",
                  },
                ]),
                barBorderRadius: 4,
              },
            },
            data: this.chartData.yData1,
          },
          {
            name: "通过且有意见",
            type: "bar",
            barWidth: 18,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 1,
                    color: "#0bceb3",
                  },
                  {
                    offset: 1,
                    color: "#0bceb3",
                  },
                ]),
                barBorderRadius: 4,
              },
            },
            data: this.chartData.yData2,
          },
          {
            name: "人均办件量",
            type: "line",
            // yAxisIndex: 1, // 使用的 y 轴的 index，在单个图表实例中存在多个 y轴的时候有用
            // showAllSymbol: true, // 显示所有图形。
            symbol: "circle", // 标记的图形为实心圆
            symbolSize: 5, // 标记的大小
            itemStyle: {
              // 折线拐点标志的样式
              color: "#ffb12a",
              borderColor: "#ffb12a",
              width: 2,
              //   shadowColor: '#ffb12a',
              //   shadowBlur: 2
            },
            lineStyle: {
              color: "#ffb12a",
              width: 2,
              shadowBlur: 2,
            },
            data: this.chartData.yData3,
          },
        ],
      };

      // 基于准备好的dom，初始化echarts实例
      this.chart = echarts.init(
        document.getElementById(`hisorline${this.getId}`)
      );
      this.chart.setOption(this.scoreOption);

      window.addEventListener("resize", this.onResize);
    },
    onResize() {
      this.chart.resize();
    },
    // 循环显示tooltip
    dynamic(chart, op, sec) {
      op.currentIndex = -1;
      let timer;
      timer = setInterval(function () {
        let dataLen = op.series[0].data.length;
        // 取消之前高亮的图形
        chart.dispatchAction({
          type: "downplay",
          seriesIndex: 0,
          dataIndex: op.currentIndex,
        });
        op.currentIndex = (op.currentIndex + 1) % dataLen;
        // 高亮当前图形
        chart.dispatchAction({
          type: "highlight",
          seriesIndex: 0,
          dataIndex: op.currentIndex,
        });

        // 显示 tooltip
        chart.dispatchAction({
          type: "showTip",
          seriesIndex: 0,
          dataIndex: op.currentIndex,
        });
      }, sec);
      return timer;
    },
  },
};
</script>
  
    <style lang="scss" scoped>
.hisorlineContainer {
  width: 100%;
  height: 100%;
}
</style>
  
  